﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Pub.Lib;
using Pub;
using Pub.Tools;

namespace MyAPP
{
    public partial class FrmChangePwd : Pub.Controls.MastForm.BaseDlg
    {
        public FrmChangePwd()
        {
            InitializeComponent();
        }

        private void FrmChangePwd_Load(object sender, EventArgs e)
        {
            lblUser.Text = PubVar.NowUser.cUserCode + "(" + PubVar.NowUser.cUserName + ")";
            this.Show();
            txtOldPWD.Focus();
        }

        /// <summary>
        /// 检查输入是否合法
        /// </summary>
        /// <returns></returns>
        private bool checkInput()
        {
            if (this.txtOldPWD.Text.Trim() == "")
            {

                Pub.Tools.MsgTools.InfoMsg("请输入旧密码.");
                txtOldPWD.Focus();
                return false;

            }
            if (this.txtNewPWD.Text == "")
            {
                Pub.Tools.MsgTools.InfoMsg("请输入新密码.");
                txtNewPWD.Focus();
                return false;

            }
            if (this.txtNewPWD2.Text == "")
            {
                Pub.Tools.MsgTools.InfoMsg("请输入确认新密码.");
                txtNewPWD2.Focus();
                return false;

            }

            if (this.txtNewPWD.Text != this.txtNewPWD2.Text)
            {
                Pub.Tools.MsgTools.InfoMsg("新密码与确认新密码不一致,请检查.");
                txtNewPWD.Focus();
                return false;
            }

            return true;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!checkInput()) { return; }
            string strMess = string.Empty;
            string strUserGuid = SysTools.SpiltArr(PubVar.SysArrAuth, PubVar.SysSplit, 2);
            ////0用户名,1密码,2GUID,3新密码
            string[] strArray = { lblUser.Text, txtOldPWD.Text, strUserGuid,txtNewPWD.Text };
            string strArrAuth = string.Join(PubVar.SysSplit, strArray, 0, strArray.Length);

            if (Helper.ADHelper.ChangeUserPasswd(strArrAuth, ref strMess))
            {
                Pub.Tools.MsgTools.InfoMsg("用户密码更改成功.");
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MsgTools.InfoMsg(strMess);
                txtOldPWD.Focus();
            }
        }

    }
}
